package com.shp.service.serviceImpl;

import com.shp.dao.PurseDao;
import com.shp.pojo.Purse;
import com.shp.pojo.Recharge;
import com.shp.pojo.Withdrawals;
import com.shp.service.PurseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @author zwl
 * @version 1.0
 * @date 2020/2/28 13:21
 */
@Service(value = "purseService")
public class PurseServiceImpl implements PurseService {

    @Autowired
    private PurseDao purseDao;

    /**
     * 根据用户的id，查询用户的钱包
     *
     * @param userId 用户id
     * @return Purse
     */
    @Override
    public Purse getPurseByUserId(Integer userId) {
        return purseDao.getPurseByUserId(userId);
    }

    /**
     * 根据Recharge，修改用户的Recharge
     *
     * @param recharge1 Recharge
     * @return true修改成功，false修改失败
     */
    @Override
    public boolean insertRecharge(Recharge recharge1) {
        if (purseDao.insertRecharge(recharge1) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据Withdrawals，修改用户的Withdrawals
     *
     * @param withdrawals1 Withdrawals
     * @return true修改成功，false修改失败
     */
    @Override
    public boolean insertWithdrawals(Withdrawals withdrawals1) {
        return purseDao.insertWithdrawals(withdrawals1) > 0;
    }

    /**
     * 查询所有的钱包记录，配合分页
     *
     * @return List<Purse>
     */
    @Override
    public List<Purse> getAll() {
        return purseDao.getAllPurse();
    }

    /**
     * 修改钱包的信息
     *
     * @param purse 钱包信息
     * @return 修改成功返回true；否则返回false
     */
    @Override
    public boolean update(Purse purse) {
        return purseDao.update(purse) == 1;
    }

    /**
     * 根据钱包id获取钱包的信息
     *
     * @param id 钱包id
     * @return Purse
     */
    @Override
    public Purse getPurseById(Integer id) {
        return purseDao.getPurseById(id);
    }

    /**
     * 根据邮箱或者状态查询钱包
     *
     * @param email
     * @param status
     * @return List<Purse>
     */
    @Override
    public List<Purse> getSelectedPurse(String email, Integer status) {
        return purseDao.getSelectedPurse(email, status);
    }

    /**
     * 获取所有的提现记录，便于分页
     *
     * @return List<Withdrawals>
     */
    @Override
    public List<Withdrawals> getWithdrawalsAll() {
        return purseDao.getWithdrawalsList();
    }

    /**
     * 提现审核
     *
     * @param withdrawals Withdrawals
     * @return true审核成功；否则审核失败
     */
    @Override
    public boolean withdrawalsPass(Withdrawals withdrawals) {
        Byte status = withdrawals.getStatus();
        Integer id = withdrawals.getId();
        // 审核通过
        if (status == 1) {
            Integer user_id = withdrawals.getUser_id();
            Float withdrawals1 = withdrawals.getWithdrawals();
            // 查询钱包
            Purse purse = purseDao.getPurseByUserId(user_id);
            Float balance = purse.getBalance();
            // 钱包充足
            if (balance >= withdrawals1) {
                // 修改状态码
                if (purseDao.updateWithdrawalsStatus(1, id) > 0) {
                    purse.setBalance(balance - withdrawals1);
                    // 修改用户钱包
                    purseDao.update(purse);
                    // 修改成功
                    return true;
                }
            }
            // 钱不足或者修改失败
            return false;
        } else if (status == 0) {
            // 审核不通过
            return purseDao.updateWithdrawalsStatus(2, id) == 1;
        } else {
            // 修改失败
            return false;
        }
    }

    /**
     * 提现选择分页
     *
     * @param email  用户邮箱
     * @param status 用户提现的状态
     * @return List<Withdrawals>
     */
    @Override
    public List<Withdrawals> getSelectedWithdrawals(String email, Byte status) {
        return purseDao.getSelectedWithdrawals(email, status);
    }

    /**
     * 批量删除提现记录
     *
     * @param ids id列表
     * @return true代表删除成功；否则删除失败
     */
    @Override
    public boolean deleteWithdrawalsBatch(Integer[] ids) {
        return purseDao.deleteWithdrawalsBatch(ids) > 0;
    }

    /**
     * 获取所有充值记录，便于分页
     *
     * @return List<Recharge>
     */
    @Override
    public List<Recharge> getRechargeAll() {
        return purseDao.getRechargeList();
    }

    /**
     * 充值审核
     *
     * @param recharge com.shp.pojo.Recharge
     * @return true审核成功；否则审核失败
     */
    @Override
    public boolean rechargePass(Recharge recharge) {
        Byte status = recharge.getStatus();
        Integer id = recharge.getId();
        // 审核通过
        if (status == 1) {
            Integer user_id = recharge.getUser_id();
            Float recharge1 = recharge.getRecharge();
            // 查询钱包
            Purse purse = purseDao.getPurseByUserId(user_id);
            Float balance = purse.getBalance();
            // 修改状态码
            if (purseDao.updateRechargeStatus(1, id) > 0) {
                purse.setBalance(balance + recharge1);
                // 修改用户钱包
                purseDao.update(purse);
                // 修改成功
                return true;
            }
            // 钱不足或者修改失败
            return false;
        } else if (status == 0) {
            // 审核不通过
            return purseDao.updateRechargeStatus(2, id) == 1;
        } else {
            // 修改失败
            return false;
        }
    }

    /**
     * 充值选择分页
     *
     * @param email  用户email
     * @param status 审核状态
     * @return List<Recharge>
     */
    @Override
    public List<Recharge> getSelectedRecharge(String email, Byte status) {
        return purseDao.getSelectedRecharge(email,status);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids id列表
     * @return true代表删除成功；否则代表删除失败
     */
    @Override
    public boolean deleteRechargeBatch(Integer[] ids) {
        return purseDao.deleteRechargeBatch(ids) > 0;
    }

    /**
     * 根据充值id，获取充值记录
     *
     * @param orderId 充值id
     * @return Recharge
     */
    @Override
    public Recharge getRecharge(String orderId) {
        return null;
    }
}
